package com.neuedu.service.impl;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastImageFile;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.neuedu.pojo.FundApply;
import com.neuedu.mapper.FundApplyMapper;
import com.neuedu.service.IFundApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 项目资金申请 服务实现类
 * </p>
 *
 * @author liuyong
 * @since 2020-09-17
 */
@Service
public class FundApplyServiceImpl extends ServiceImpl<FundApplyMapper, FundApply> implements IFundApplyService {
    @Resource
    FastFileStorageClient fastFileStorageClient;
    @Override
    public boolean add(FundApply fundApply) throws IOException {
        MultipartFile file=fundApply.getMultipartFile();
        FastImageFile fastImageFile=new FastImageFile(
                file.getInputStream(),
                file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()),
                new HashSet<>()
        );
        StorePath storePath=fastFileStorageClient.uploadImage(fastImageFile);
        fundApply.setApplyFileUrl(storePath.getFullPath());
        return  this.saveOrUpdate(fundApply);
    }

    @Override
    public List<FundApply> getByProjectId(int pid) {
        return getBaseMapper().getByProjectId(pid);
    }

}
